package controllers;

import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.CustomerService;
import services.GalleryService;
import domain.Customer;
import domain.Gallery;

@Controller
@RequestMapping("/gallery")
public class GalleryController extends AbstractController {

	//Services
	@Autowired
	private GalleryService galleryService;
	@Autowired
	private CustomerService customerService;
	
	//Constructor
	public GalleryController(){
		super();
	}
	
	// Listing methods
	@RequestMapping(value = "/listAll", method = RequestMethod.GET)
	public ModelAndView list() {
		ModelAndView result;
		Collection<Gallery> galleries;

		galleries = galleryService.findAll();

		result = new ModelAndView("gallery/list");
		result.addObject("requestURI", "gallery/listAll.do");
		result.addObject("galleries", galleries);
		result.addObject("backURI", "/welcome/index.do");
		return result;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView listCustomer(@RequestParam int customerId) {
		ModelAndView result;
		Customer customer;
		Collection<Gallery> galleries;

		customer = customerService.findOne(customerId);
		galleries = galleryService.findByCustomer(customer);

		result = new ModelAndView("gallery/list");
		result.addObject("requestURI", "gallery/list.do?customerId=" + customerId);
		result.addObject("galleries", galleries);
		result.addObject("backURI", "/customer/list.do");
		return result;
	}
}
